package org.gpp.proj1.test;

import java.util.concurrent.Semaphore;

import org.fenggui.composites.Window;

import com.jme.util.LoggingSystem;

/**
 * Facade class for the menu system's easyaccept tests. It uses
 * a BaseGame-type class to handle the input events and gather the
 * output data 
 * 
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class MenuSystemFacade implements JMEInitListener {
	
	private MenuTestCanvas testCanvas;
	
	private Semaphore jmeInitSemaphore;

	public MenuSystemFacade() {
		// Disabling logging
		LoggingSystem.getLoggingSystem().loggerOn( false );
		
		// Initializing JME
		initJME();
	}

	private void initJME() {
		this.jmeInitSemaphore = new Semaphore( 0 );
		
		this.testCanvas = new MenuTestCanvas();
		this.testCanvas.addInitListener( this );
		
		new GameCanvasThread( testCanvas ).start();
		
		try {
			this.jmeInitSemaphore.acquire();
		} catch (InterruptedException e) {
			throw new RuntimeException( "Could not initialize JME", e );
		}
	}
	
	/**
	 * Thread used to execute the canvas class
	 * 
	 * @author Paolo Victor, paolovictor@gmail.com
	 */
	class GameCanvasThread extends Thread {
		private MenuTestCanvas canvas;

		public GameCanvasThread( MenuTestCanvas canvas ) {
			this.canvas = canvas;
		}
		
		@Override
		public void run() {
			canvas.start();
		}
	}

	public void jmeInitSuccessful() {
		
		this.jmeInitSemaphore.release();
	}
	
	/////////////////////////////////////////////////////////////////////////////////
	// TEST METHODS
	/////////////////////////////////////////////////////////////////////////////////	
	
	
	public void createWindow( String widgetId, String frameTitle, int x, int y, int w, int h ) {
		testCanvas.createWindow( widgetId, frameTitle, x, y, w, h );
	}
	
	public String getWidgetTitle( String widgetId ) {
		Window window = (Window) testCanvas.getWidget( widgetId );
		return window.getTitle();
	}
	
	public int getWidgetPositionX( String widgetId ) {
		return testCanvas.getWidget( widgetId ).getDisplayX();
	}
	
	public int getWidgetPositionY( String widgetId ) {
		return testCanvas.getWidget( widgetId ).getDisplayY();
	}
	
	public int getWidgetWidth( String widgetId ) {
		return testCanvas.getWidget( widgetId ).getWidth();
	}
	
	public int getWidgetHeight( String widgetId ) {
		return testCanvas.getWidget( widgetId ).getHeight();
	}
	
	public void pressMouseLeftButton( int x, int y ) {
		// 0 is the code for "left mouse button"
		testCanvas.getInputHandler().onButton( 0, true, x, y);
		testCanvas.refresh();
	}
	
	public void releaseMouseLeftButton( int x, int y ) {
		// 0 is the code for "left mouse button"
		testCanvas.getInputHandler().onButton( 0, false, x, y);
		testCanvas.refresh();
	}
		
	public void moveMouse( int newX, int newY ) {
		testCanvas.getInputHandler().onMove( 0, 0, newX, newY );
		testCanvas.refresh();
	}
	
	public void wait( int time ) {
		try {
			Thread.sleep( time );
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}